package com.lyh.web.controller.pay;

import com.alibaba.fastjson.JSON;
import com.lyh.common.annotation.Log;
import com.lyh.common.enums.BusinessType;
import com.lyh.system.domain.Order;
import com.lyh.system.domain.RechargeRecord;
import com.lyh.system.domain.req.ZfbCashAdvanceReq;
import com.lyh.system.domain.req.zfbRefundReq;
import com.lyh.system.mapper.OrderMapper;
import com.lyh.system.mapper.RechargeRecordMapper;
import com.lyh.system.service.IPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/yun")
@Api(tags = "网站支付宝支付")
@Slf4j
public class AliPayController {

    @Autowired
    private IPayService alipayService;


    @Autowired
    private  OrderMapper  orderMapper;

    @Autowired
    private RechargeRecordMapper rechargeRecordMapper;

    @ApiOperation("统一收单下单并支付接口调用--测试")
    @GetMapping("/scanPay")
    public String tradePagePay() throws Exception {
        log.info("统一收单下单并支付接口调用");
        /*
        支付宝开放平台接收 request 请求对象后
        会为开发者生成一个html形式的form表单,包含自动提交的脚本
         */
        String ajaxResult = alipayService.tradeCreate();
        /*
        将form表单字符串返回给前端
        前端自动提交脚本
        表单会自动提交到 action熟悉只想的支付宝开放平台中 为用户展示一个支付页面
         */
        return ajaxResult;
    }

    @ApiOperation("统一收单下单并支付接口调用--测试")
    @GetMapping("/testZfb")
    public String testZfb(@RequestParam String orderId) throws Exception {
        log.info("统一收单下单并支付接口调用");
        //Order order = orderMapper.selectById(orderId);

        RechargeRecord rechargeRecord = rechargeRecordMapper.selectById(orderId);

        Order order = new Order();
        order.setId(rechargeRecord.getId());
        order.setOrderNum(rechargeRecord.getOrderId());
        order.setActualPayment(rechargeRecord.getRechargeAmount());
        String s = alipayService.zfbPay(order);
        return s;
    }

    @ApiOperation("支付宝退款接口调用--订单")
    @GetMapping("/zfbRefund")
    @Log(title = "支付宝退款接口调用", businessType = BusinessType.INSERT)
    public String zfbRefund(@RequestBody @Validated zfbRefundReq req){
        log.info("统一收单退款接口调用");
        Order order = orderMapper.selectById(req.getOrderId());
        String s = alipayService.zfbRefund(order);
        return s;
    }
    /**
     * 支付宝回调
     *
     * @param params
     * @return
     */
    @ApiOperation("交易通知")
    @PostMapping("/tradeNotify")
    @Log(title = "支付宝支付回调", businessType = BusinessType.INSERT)
    public String tradeNotify(@RequestParam  Map<String, String> params) {
       /* Map<String,String> params = new HashMap<String,String>();
        Map requestParams = request.getParameterMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
            //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
            params.put(name, valueStr);
        }*/
        log.info("支付通知,正在执行,通知参数:{}", JSON.toJSONString(params));
        return alipayService.tradeNotify(params);
    }

    /**
     * 支付宝回调
     *
     * @param params
     * @return
     */
    @ApiOperation("支付宝转账提现")
    @PostMapping("/zfbCashAdvance")
    @Log(title = "支付宝转账提现", businessType = BusinessType.INSERT)
    public boolean zfbCashAdvance(@RequestBody ZfbCashAdvanceReq req) {
        log.info("支付宝转账提现,正在执行,通知参数:{}", JSON.toJSONString(req));
        return alipayService.zfbCashAdvance(req);
    }


}
